;!
; Journeyman
;
; Stephen Carmody
;
;

#include <Misc.au3>


Global $Window
Global $HotKey[128]
Global $Tasks[1]
Global $TaskIndex = 0
Global $TaskLast = 0
Global $TaskNext = 0
Global $TaskKey
Global $Quit = False
Global $Pause = False
Global $Binded
Global $Listening = False

; Read the configuration file
Global $Ini = IniReadSection("journeyman.ini", "journeyman")
If @error Then
    MsgBox(4096, "", "Error reading INI file.")
	Exit 0
EndIf

For $i = 1 To $Ini[0][0]
	$Key = $Ini[$i][0]
	$Val = $Ini[$i][1]	
	Switch $Key
	Case "KeyDelay"
		Opt("SendKeyDelay", $Val)
		Opt("SendKeyDownDelay", 0)
	Case "Window"
		$Window = $Val
	Case "Listen"
		HotKeySet($Val, "Listen")
	Case Else
		$HotKey[Asc($Key)] = $Val
		$Binded = $Binded & $Key
	EndSwitch
Next

Func a()
	KeyPressed("a", Asc("a"))
EndFunc
Func b()
	KeyPressed("b", Asc("b"))
EndFunc
Func c()
	KeyPressed("c", Asc("c"))
EndFunc
Func d()
	KeyPressed("d", Asc("d"))
EndFunc
Func e()
	KeyPressed("e", Asc("e"))
EndFunc
Func f()
	KeyPressed("f", Asc("f"))
EndFunc
Func g()
	KeyPressed("g", Asc("g"))
EndFunc
Func h()
	KeyPressed("h", Asc("h"))
EndFunc
Func i()
	KeyPressed("i", Asc("i"))
EndFunc
Func j()
	KeyPressed("j", Asc("j"))
EndFunc
Func k()
	KeyPressed("k", Asc("k"))
EndFunc
Func l()
	KeyPressed("l", Asc("l"))
EndFunc
Func m()
	KeyPressed("m", Asc("m"))
EndFunc
Func n()
	KeyPressed("n", Asc("n"))
EndFunc
Func o()
	KeyPressed("o", Asc("o"))
EndFunc
Func p()
	KeyPressed("p", Asc("p"))
EndFunc
Func q()
	KeyPressed("q", Asc("q"))
EndFunc
Func r()
	KeyPressed("r", Asc("r"))
EndFunc
Func s()
	KeyPressed("s", Asc("s"))
EndFunc
Func t()
	KeyPressed("t", Asc("t"))
EndFunc
Func u()
	KeyPressed("u", Asc("u"))
EndFunc
Func v()
	KeyPressed("v", Asc("v"))
EndFunc
Func w()
	KeyPressed("w", Asc("w"))
EndFunc
Func x()
	KeyPressed("x", Asc("x"))
EndFunc
Func y()
	KeyPressed("y", Asc("y"))
EndFunc
Func z()
	KeyPressed("z", Asc("z"))
EndFunc


Func Listen()
	$Listening = Not $Listening
	If $Listening Then
		For $i = 1 To StringLen($Binded)
			$Key = StringMid($Binded, $i, 1)
			HotKeySet($Key, $Key)
		Next
	Else
		For $i = 1 To StringLen($Binded)
			$Key = StringMid($Binded, $i, 1)
			HotKeySet($Key)
		Next
	EndIf
EndFunc



Func KeyPressed($Key, $Val)
	Listen()
	$Cmd = $HotKey[$Val] 
	If $Cmd Then
		$Sequence = StringSplit($Cmd, "|")		
		If $Sequence[0] == 1 Then
			Task($Sequence[1])
		ElseIf Not $Pause And $TaskKey == $Key Then
			$TaskKey = ""
			$Tasks[0] = 0
		ElseIF Not $Pause Then
			$Tasks = $Sequence
			$TaskIndex = 1
			$TaskLast = TimerInit()
			$TaskNext = 0
			$TaskKey = $Key
		EndIf
	EndIf
EndFunc



Func SendKeys($Keys)
	If $Pause Then
		Return
	EndIf
	Send($Keys)
EndFunc



Func Task($Task)
	$KeysWait = StringSplit($Task, "...", 1)
	If $KeysWait[0] > 0 Then
		$Keys = $KeysWait[1]
		If $Keys == "{quit}" Then
			$Quit = True
		ElseIf $Keys == "{pause}" Then
			$Pause = Not $Pause
		ElseIf StringLeft($Keys, 6) == "{next " Then
			$Next = StringSplit($Keys, " ")
			If $Next[0] > 1 Then
				$TaskIndex = Int(StringLeft($Next[2], StringLen($Next[2]) - 1)) - 1
			EndIf
		Else
			SendKeys($KeysWait[1])
		EndIf
		If $KeysWait[0] > 1 Then
			Return $KeysWait[2]
		EndIf
	EndIf
	Return 0
EndFunc



Func Update()
    If $Tasks[0] > 0 And TimerDiff($TaskLast) >= $TaskNext Then
		$TaskLast = TimerInit()
		$TaskNext = Task($Tasks[$TaskIndex])
		$TaskIndex = $TaskIndex + 1
		If $TaskIndex == $Tasks[0] + 1 Then
			$Tasks[0] = 0
		EndIf
    EndIf
    Sleep(100)
EndFunc



While Not $Quit
	Update()
WEnd